/** 
 ** Copyright (c) 2010 Ushahidi Inc
 ** All rights reserved
 ** Contact: team@ushahidi.com
 ** Website: http://www.ushahidi.com
 ** 
 ** GNU Lesser General Public License Usage
 ** This file may be used under the terms of the GNU Lesser
 ** General Public License version 3 as published by the Free Software
 ** Foundation and appearing in the file LICENSE.LGPL included in the
 ** packaging of this file. Please review the following information to
 ** ensure the GNU Lesser General Public License version 3 requirements
 ** will be met: http://www.gnu.org/licenses/lgpl.html.	
 **	
 **
 ** If you have questions regarding the use of this file, please contact
 ** Ushahidi developers at team@ushahidi.com.
 ** 
 **/

package com.ushahidi.android.app.net;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpResponseException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.ushahidi.android.app.UshahidiPref;

public class LinkedResourceNetRequest {
	
    	//private final static String incidentCateArg = "&by=cattype&category_type=1";
    	//private final static String resourceCateArg = "&by=cattype&category_type=2";
    	    	    
	public static JSONArray getAllLinkedResourceFromWebByID(String InID)
	{
	    StringBuilder uriBuilder = new StringBuilder( UshahidiPref.domain);
	    if(!UshahidiPref.domain.endsWith("/"))
		uriBuilder.append("/");
	    uriBuilder.append("api?task=queryInLink&InID="+InID+"&sort=0");
	    //modified by yujie, fetch all resource
	    return 
	    	getResourceFromWeb(uriBuilder.toString());
	}

	public static JSONArray getResourceFromWeb(String url)
	{
	    String resource = "";
	    HttpResponse response=null;
	    
	    try{
	     response = UshahidiHttpClient.GetURL( url);
	    }
	    catch(HttpResponseException e) {
	        System.err.println(e.toString());
	    }
		catch(Exception e)
		{
		    
		    System.err.println(e.toString());
		}
	    Log.v("yujie", "Request:["+url+"]");
	    
	    if( response == null ) {
		return null;
	    }
		
	    final int statusCode = response.getStatusLine().getStatusCode();
		
	    if( statusCode == 200 ) {
	    	resource = UshahidiHttpClient.GetText(response).toString();
		    try
            {
		        JSONArray result = new JSONObject(resource).getJSONObject("payload").getJSONArray("resource_categories");
		        return result;
            }
            catch (JSONException e)
            {
	        	Log.v("Oceanmaster", "Error occured during parsing JSONObject!!");
		        return null;
            }
		    
	    } else {
		return null;
	    }
	}
}
